package com.gosophia.conferenceManagement.confPreparation.ws.impl;

import java.util.ArrayList;
import java.util.List;

import org.dozer.DozerBeanMapper;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.gosophia.commons.DozerChangeUtil;
import com.gosophia.commons.exception.BusinessException;
import com.gosophia.conferenceManagement.common.dto.DocumentDTO;
import com.gosophia.conferenceManagement.common.entity.Document;
import com.gosophia.conferenceManagement.confPreparation.dto.ParticipatingNameListDTO;
import com.gosophia.conferenceManagement.confPreparation.entity.NotificationFeedbackList;
import com.gosophia.conferenceManagement.confPreparation.entity.ParticipatingNameList;
import com.gosophia.conferenceManagement.confPreparation.service.ConfPreparationService;
import com.gosophia.conferenceManagement.confPreparation.ws.ConfPreparationWebService;
import com.gosophia.organizationManagement.entity.Position;
import com.gosophia.organizationManagement.service.OrganizationManagementService;
import com.gosophia.userManagement.dto.UserInfoDTO;
import com.gosophia.userManagement.entity.UserInfo;
import com.gosophiaigit.common.busi.service.IGroupUtilService;
import com.gosophiaigit.contact.busi.entity.PositionUser;
/**
 * 会务管理webService实现
 * @author wangxinbing
 * @since  2011-3-25
 *  Copyright (c) 2010 Gosophia corporation All Rights Reserved
 */
@Transactional
@Service("confPreparationWebService")
public class ConfPreparationWebServiceImpl implements ConfPreparationWebService {
	
	@Autowired
	private ConfPreparationService confPreparationService;
	@Autowired
	private OrganizationManagementService organizationManagementService;
	@Autowired
	private IGroupUtilService groupUtilService;
    
    private Mapper dozer = new DozerBeanMapper();
	@Override
	public void addNotificationFeedbackList(Long jobId,
			List<UserInfoDTO> userList) throws BusinessException {
		Assert.notNull(jobId,"jobId不能为null");
		List<UserInfo> userInfoList =new ArrayList<UserInfo>();
		for(UserInfoDTO udto : userList){
			UserInfo user = new UserInfo();
			user.setUserInfoId(udto.getUserInfoId());
			userInfoList.add(user);
		}
		confPreparationService.addNotificationFeedbackList(jobId, userInfoList);
	}

	@Override
	public DocumentDTO findAgendaByJobId(Long jobId) throws BusinessException{
		Assert.notNull(jobId,"jobId不能为null");
		Document doc = confPreparationService.findAgendaByJobId(jobId);
		DocumentDTO docDTO = new DocumentDTO();
		if(doc!=null){
			docDTO = DozerChangeUtil.changeBeforeToAfter(doc, DocumentDTO.class);
		}
		return docDTO;
	}

	@Override
	public List<DocumentDTO> findMaterialsByJobId(Long jobId) throws BusinessException{
		Assert.notNull(jobId,"jobId不能为null");
		List<Document> docList = confPreparationService.findMaterialsByJobId(jobId);
		List<DocumentDTO> docDTOList = new ArrayList<DocumentDTO>();
		if(docList!=null){
			docDTOList = DozerChangeUtil.changeBeforeToAfter(docList, DocumentDTO.class);
		}
		return docDTOList;
	}

	@Override
	public List<ParticipatingNameListDTO> findNotificationFeedbackListsByJobId(
			Long jobId) {
		//TODO 反馈单
	    //修改于2011-4-25
		Assert.notNull(jobId,"jobId不能为null");
		List<NotificationFeedbackList> notifyLists = confPreparationService.
		                           findNotificationFeedbackListsByJobId(jobId);
		List<ParticipatingNameListDTO> pListDTOs = new ArrayList<ParticipatingNameListDTO>();
		for(NotificationFeedbackList notify : notifyLists){
			ParticipatingNameListDTO pDTO = new ParticipatingNameListDTO();
			pDTO.setUserName(notify.getUserInfo().getUserName());
			PositionUser pUser = groupUtilService.getPositionUserByUserinfoId(notify.getUserInfo().getUserInfoId());
			Long positionId = pUser.getPositionId();
			Position position = new Position();
			if(positionId!=null){
				position  =	organizationManagementService.findPositionDetailBypositionId(positionId);
				pDTO.setPositionName(position.getPositionName());
				pDTO.setOrgName(position.getOrganization().getOrgName());
			}
			//pDTO.setPositionName(notify.getUserInfo().getPosition());
			//pDTO.setOrgName(notify.getUserInfo().getOrganization());
			pDTO.setIfAttend(notify.getIfAttend());
			pListDTOs.add(pDTO);
		}
		return pListDTOs;
	}

	@Override
	public List<ParticipatingNameListDTO> findParticipatingNameListsByJobId(
			Long jobId) {
		Assert.notNull(jobId,"jobId不能为null");
		List<ParticipatingNameListDTO> pListDTOs = new ArrayList<ParticipatingNameListDTO>();
		List<ParticipatingNameList> plist = confPreparationService.findParticipatingNameListsByJobId(jobId);
		if(plist !=null){
			pListDTOs = DozerChangeUtil.changeBeforeToAfter(plist, ParticipatingNameListDTO.class);
		}
		return pListDTOs;
	}

	@Override
	public void updateNotificationFeedbackList(Long jobId, Long userId,
			String ifAttend) throws BusinessException {
		Assert.notNull(jobId,"jobId不能为null");
		Assert.notNull(userId,"userId不能为null");
		Assert.notNull(ifAttend,"ifAttend不能为null");
		confPreparationService.updateNotificationFeedbackList(jobId, userId, ifAttend);
	}

	@Override
	public void updateParticipatingNameList(Long jobId,
			List<ParticipatingNameListDTO> participatingNameLists)
			throws BusinessException {
		Assert.notNull(jobId,"jobId不能为null");
		List<ParticipatingNameList> plist = new ArrayList<ParticipatingNameList>();
		plist = DozerChangeUtil.changeBeforeToAfter(participatingNameLists, ParticipatingNameList.class);
		confPreparationService.updateParticipatingNameList(jobId, plist);
	}

	@Override
	public DocumentDTO findSignTableByJodId(Long jobId)
			throws BusinessException {
		Assert.notNull(jobId,"jobId不能为null");
		Document doc = confPreparationService.findSignTableByJodId(jobId);
		DocumentDTO docDTO = new DocumentDTO();
		if(doc!=null){
			docDTO = DozerChangeUtil.changeBeforeToAfter(doc, DocumentDTO.class);
		}
		return docDTO;
	}

	@Override
	public void updateConfMaterials(Long jobId, List<DocumentDTO> materials)
			throws BusinessException {
		Assert.notNull(jobId,"jobId不能为null");
		List<Document> materialsDocS = new ArrayList<Document>();
		for(DocumentDTO docDTO : materials){
			Document doc = new Document();
			doc.setDocumentCategory(docDTO.getDocumentCategory());
			doc.setDocumentName(docDTO.getDocumentName());
			doc.setDocumentNote(docDTO.getDocumentNote());
			doc.setDocumentPath(docDTO.getDocumentPath());
			materialsDocS.add(doc);
		}
		confPreparationService.updateConfMaterials(jobId,materialsDocS);
		
	}

	@Override
	public DocumentDTO updateAgenda(Long jobId, DocumentDTO docDTO)
			throws BusinessException {
		Assert.notNull(jobId,"jobId不能为null");
		Document doc = new Document();
		doc.setDocumentCategory(docDTO.getDocumentCategory());
		doc.setDocumentName(docDTO.getDocumentName());
		doc.setDocumentNote(docDTO.getDocumentNote());
		doc.setDocumentPath(docDTO.getDocumentPath());
		doc.setDocumentVersion(docDTO.getDocumentVersion());
		doc = confPreparationService.updateAgenda(jobId,doc);
		docDTO = DozerChangeUtil.changeBeforeToAfter(doc, DocumentDTO.class);
		return docDTO;
	}

	@Override
	public DocumentDTO updateConfSignTable(Long jobId, DocumentDTO docDTO)
			throws BusinessException {
		Assert.notNull(jobId,"jobId不能为null");
		Document doc = new Document();
		doc.setDocumentCategory(docDTO.getDocumentCategory());
		doc.setDocumentName(docDTO.getDocumentName());
		doc.setDocumentNote(docDTO.getDocumentNote());
		doc.setDocumentPath(docDTO.getDocumentPath());
		doc.setDocumentVersion(docDTO.getDocumentVersion());
		doc = confPreparationService.updateSignTable(jobId,doc);
		docDTO = DozerChangeUtil.changeBeforeToAfter(doc, DocumentDTO.class);
		return docDTO;
	}

	@Override
	public void accomplishConferencePrepare(Long jobId,Long operatorId,String confPrepareType)
			throws BusinessException {
		Assert.notNull(jobId,"jobId不能为null");
		confPreparationService.accomplishConferencePrepare(jobId,operatorId,confPrepareType);
		
	}
	
	@Override
	public boolean ifFeedBacked(Long jobId, Long userInfoId) {
		Assert.notNull(jobId,"jobId不能为null");
		Assert.notNull(userInfoId,"userInfoId不能为null");
		return confPreparationService.ifFeedBacked(jobId, userInfoId);
	}

	@Override
	public void deleteConfPMetailBydocId(Long documentId,Long jobId)
			throws BusinessException {
		Assert.notNull(documentId,"documentId不能为null");
		Assert.notNull(jobId,"jobId不能为null");
		confPreparationService.deleteConfPMetailBydocId(documentId,jobId);
	}

}
